package MineGod;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.Rectangle;

public class EntityLava extends Entity{
	public double durability;
	public double maxDurability;
	public double y;
	public static double posYSpeed = .1;
	public static double posXSpeed = .1;
	public double xSpeed;
	public double ySpeed;
	
	
	public EntityLava(double xMove, double yMove, double x, double y, double dur){
		durability = dur;
		maxDurability = 1;
		this.x = x;
		this.y = y;
		this.zIndex = 1;
		dead = false;
		
		xSpeed = posXSpeed*xMove;
		ySpeed = posYSpeed*yMove;
		
		mask = new Rectangle((float)(x), (float)(y), (float)BlockSand.WIDTH, (float)BlockSand.WIDTH);
	}
	@Override
	public void interactWithPlayerOnCollision(Player player) {
		// TODO Auto-generated method stub
		player.damage(15, DamageType.lava);
	}

	@Override
	public void doLogic(int delta, Chunk prevChunk, Chunk currChunk, Chunk nextChunk) {
		// TODO Auto-generated method stub
		
		
		//Change current block to block lava
		Block b = new BlockLava();
		b.durability = durability;
		Utils.changeCurrentBlock(b, x, y);
		
		int moveX = 0;
		int moveY = 0;
		
		if (ySpeed != 0){
			moveY = 1;
		}
		if (xSpeed < 0){
			moveX = -1;
		}else if (xSpeed > 0){
			moveX = 1;
		}
		if (moveY==0){
			Block b3 = Utils.pointToBlock(x, y+Block.WIDTH);
			if (b3!=null && b3.type == BlockType.air){
				dead = true;
				Utils.delayAddEntity(new EntityLava(0,1,x - (x%Block.WIDTH),y - (y%Block.WIDTH),durability));
				return;
			}
		}
		Block b2 = Utils.pointToBlock(x+Block.WIDTH*moveX, y+Block.WIDTH*moveY);
		if (b2==null || (b2.type != BlockType.air && !(b2.type == BlockType.lava && moveY==0))){
				dead = true;
				
				//Spawn Lava entity(s) in correct direction(s)
				if (moveY != 0){
					//Check left and right, and if possible spawn in that direction.
					b2 = Utils.pointToBlock(x+Block.WIDTH, y);
					if (b2!=null && (b2.type == BlockType.air || b2.type == BlockType.lava)){
						Utils.delayAddEntity(new EntityLava(1,0,x - (x%Block.WIDTH),y - (y%Block.WIDTH),durability));
					}
					b2 = Utils.pointToBlock(x-Block.WIDTH, y);
					if (b2!=null && (b2.type == BlockType.air || b2.type == BlockType.lava)){
						Utils.delayAddEntity(new EntityLava(-1,0,x - (x%Block.WIDTH),y - (y%Block.WIDTH),durability));
					}
					
				}else{
					//Check below, and if possible spawn in that direction
					b2 = Utils.pointToBlock(x, y+Block.WIDTH);
					if (b2!=null && b2.type == BlockType.air){
						Utils.delayAddEntity(new EntityLava(0,1,x - (x%Block.WIDTH),y - (y%Block.WIDTH),durability));
					}
					
				}
		}
		y = y+ySpeed*delta;
		x = x+xSpeed*delta;
		mask = new Rectangle((float)(x), (float)(y), (float)BlockLava.WIDTH, (float)BlockLava.WIDTH);
		
		
	}

	@Override
	public void render(double worldPos) {
		if (!(x+Block.WIDTH-worldPos<0 || x-Block.WIDTH-worldPos>Chunk.CHUNK_WIDTH*Block.WIDTH)){
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			BlockLava.lavaTex.bind();
			double renderX = x - worldPos;
			GL11.glColor3d((durability)/maxDurability, (durability)/maxDurability, (durability)/maxDurability); // Could make this faster!!
			GL11.glBegin(GL11.GL_QUADS);
				GL11.glNormal3d(0, 0, 1);
				GL11.glTexCoord2d(0.0, 0.0);
				GL11.glVertex3d(renderX, y, zIndex);
				GL11.glTexCoord2d(1.0, 0.0);
				GL11.glVertex3d(renderX + BlockSand.WIDTH, y,zIndex);
				GL11.glTexCoord2d(1.0, 1.0);
				GL11.glVertex3d(renderX + BlockSand.WIDTH, y + BlockSand.WIDTH, zIndex);
				GL11.glTexCoord2d(0.0, 1.0);
				GL11.glVertex3d(renderX, y + BlockSand.WIDTH, zIndex);
			GL11.glEnd();
		}
	}

}